package com.qf.yuandanwork.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.qf.yuandanwork.dao.UserDao;
import com.qf.yuandanwork.entity.User;
import com.qf.yuandanwork.service.UserService;
import com.qf.yuandanwork.utils.DateUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public  void writerUsers(List<User> list) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\2203-maven\\yuandanwork\\userTest.csv",true));
        for (User user : list) {
            //book转成字符串
            String s = user.toCSVString();
            bw.write(s);
            bw.newLine();
        }
        //关流
        bw.close();
    }

    @Override
    public void randomUser() throws IOException {
        //加true是再次添加不覆盖，不加或者false是覆盖
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\text\\randomUser.txt",false));


        for (int i = 0; i < 1000; i++) {
            /*
             * 随机生成多少个*/
            String user= RandomUtil.randomString(5)+","
                    +RandomUtil.randomString(10)+","
                    +RandomUtil.randomInt(0, 2)+","
                    +DateUtils.birthday()+","
                    +DateUtils.registTime()+","
                    +RandomUtil.randomString(8)+","
                    +RandomUtil.randomInt(0, 2)+","
                    +RandomUtil.randomInt(1000, 5000);


            bw.write(user);
            bw.newLine();

        }
        bw.close();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertManyUsers() throws Exception {
        //1.读取文件
        BufferedReader bf=new BufferedReader(new FileReader("D:\\text\\randomUser.txt"));
       List<User> list=new ArrayList<>();
        String str;
        int count=0;
        while ((str=bf.readLine())!=null){
            //将文件中的数据以，分割 为了存入book对象中
            String[] split = str.split(",");
            //创建book对象
            User user = new User();
            user.setName(split[0]);
            user.setPassword(split[1]);
            user.setSex(split[2]);
            user.setBirthday(DateUtils.utils(split[3]));
            user.setRegistTime(DateUtils.utils(split[4]));
            user.setAuth(split[5]);
            user.setStatus(split[6]);
            user.setAmount(Integer.parseInt(split[7]));
            //将book存入list集合中
            list.add(user);
            count++;
            //判断每200条读取一次 还有count是0的时候不可以进去否则第一次就会进去
            if (count%200==0&&count!=0){
               //userService.insertManyUsers();
                userDao.insertManyUsers(list);
                list.clear();
                count=0;
            }
        }
        //关流
        bf.close();

    }
}

